package com.hnluchuan.bmjlearning.web.controller.admin;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.hnluchuan.bmjlearning.common.*;
import com.hnluchuan.bmjlearning.dao.UserDAO;
import com.hnluchuan.bmjlearning.dao.UserRoleDAO;
import com.hnluchuan.bmjlearning.dto.*;
import com.hnluchuan.bmjlearning.common.CustomerStatus;
import com.hnluchuan.bmjlearning.model.CustomerServicePackage;
import com.hnluchuan.bmjlearning.model.User;
import com.hnluchuan.bmjlearning.model.UserRole;
import com.hnluchuan.bmjlearning.security.PasswordEncoder;
import com.hnluchuan.utils.common.DateUtils;
import com.hnluchuan.utils.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.bmjlearning.service.AgentService;
import com.hnluchuan.bmjlearning.service.CoreService;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.bmjlearning.web.controller.BaseController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/admin/bmj", produces="text/html;charset=UTF-8")
public class AdminBmjController extends BaseController {

	@Autowired
	private AgentService agentService;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private CoreService coreService;

	@RequestMapping("bp")
	public String bp(HttpServletResponse response, Map<String, Object> map) throws Exception {
		UserDTO user = getCurrentUserDTO();
		if (!coreService.isServicePackageValid(user.getId())) {
			return "admin/bmj/bmj_expired";
		} else {
			String token = coreService.generateTokenAndSetToCache(user.getId());
			Cookie c = new Cookie("Access-Token", token);
			c.setPath("/");
			response.addCookie(c);
			map.put("token", token);
		}
		return "admin/bmj/bmj_bp";
	}
	
	@RequestMapping("learning")
	public String learning(Map<String, Object> map) throws Exception {
		UserDTO user = getCurrentUserDTO();
		if (!coreService.isServicePackageValid(user.getId())) {
			return "admin/bmj/bmj_expired";
		}
		//coreService.generateTokenAndSetToCache(user.getId());
		//return "redirect:/admin/customer/learningIndex";
		return "admin/bmj/bmj_learning";
	}
	
	@RequestMapping("learningIndex")
	public String learningIndex(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) throws Exception {
		Long userId = getCurrentUserDTO().getId();
		boolean isValid = coreService.isServicePackageValid(userId);
		if (isValid) {
			String token = coreService.generateTokenAndSetToCache(userId);
			Cookie c = new Cookie("Access-Token", token);
			c.setPath("/");
			response.addCookie(c);
			UserDTO user = getCurrentUserDTO();
			//map.put("username", user.getUsername());
			//map.put("password", desService.decrypt(user.getPasswordDesEncoded(), userService.getDesKey()));
			map.put("username", getCurrentUserDTO().getUsername());
			map.put("password", "Quanke2018!");
			return "admin/customer/customer_auto_login"; // 返回在自动登陆的页面
		} else {
			return ""; // 
		}
	}
}
